// *************************************
// EFMP Fleet
// Summstat_plots.do
// *************************************

/*
// Description
// ----------------------------
This program analyses the Enhanced Fleet Modernization Program (EFMP) in 
California and its effect on the fleet.  It creates a number of plots used in the analyses, including
Figures 1, 2, 4 and Online Appendix Figure A.1
*/

/*** START CODE ***/


local CES20Threshold 36.6
local bw 1.0


/*Figure 1: MPG distribution, Light-duty vehicles purchased in CA, 2015-2017*/

use "${EFMPFleetData}/Before_collapse_tract_quarter.dta", clear
replace mpg = round(mpg,1)
keep if year~=2014
gcollapse (sum) cnt_mmy, by(mpg)
replace cnt_mmy = round(cnt_mmy/1000,1)
gen mpg_missing = mpg==.
tab mpg_missing [fweight=cnt_mmy]
set scheme s1color


graph twoway histogram mpg [fweight=cnt_mmy], freq width(2) ///
xtitle("Combined Fuel Economy (MPG)") ///
ytitle("Number of Units Sold (000s)""California, 2015-2017") ///
xlabel(0 (10) 150) 
graph export "${EFMPFleetRes}/${ResultsVersion}/MPG_distribution.png", replace


/*Figure 2: Sales and mean fuel economy of purchased vehicles*/

use "${EFMPFleetData}/After_collapse_tract_quarter.dta", clear

bys quarter year: egen total = total(cnt_total)
gcollapse (mean) gpm  [fw = cnt_total], by(quarter year total)
keep if year>2014
keep if year<2018

gen yq = yq(year, quarter)
format yq %tq
replace total = total/1000000
sort yq
gen mpg = 1/gpm


graph twoway (line total yq, lp(solid) lc(blue)) (line mpg yq, yaxis(2) lp(dash) lc(red)), ///
ylabel(0 (.5) 2) ylabel(20 (1) 25, axis(2)) ///
legend(order(1 "Units Sold" 2 "Fuel Economy")) ///
xtitle(Year-Quarter) ///
ytitle(Units Sold (millions)) ytitle(Miles per gallon, axis(2))

graph export "${EFMPFleetRes}/MPG_sales_overtime.png", replace


/*Figure 4: GPM distribution, treated and untreated zip codes.*/

use "${EFMPFleetData}/TransactionData.dta", clear
la var gpm "GPM"
drop if zip==.

	#delimit ;
	twoway  kdensity gpm if dac == 1 & aqmd == 1 & CalendarYear == 2014, lcolor(cranberry) lpattern(dot) 
		||  kdensity gpm if dac == 1 & aqmd == 1 & CalendarYear == 2015, lcolor(cranberry) lpattern(shortdash)  
		||  kdensity gpm if dac == 1 & aqmd == 1 & CalendarYear == 2016, lcolor(cranberry) lpattern(longdash)  
		||  kdensity gpm if dac == 1 & aqmd == 1 & CalendarYear == 2017, lcolor(cranberry)   
		, graphregion(color(white)) 
		legend(label(1 "2014") label(2 "2015") label(3 "2016") label(4 "2017"));
	graph export "${EFMPFleetRes}/kdensity_gpm_treated_by_year.png", replace;
	#delimit cr
	
	#delimit ;
	twoway  kdensity gpm if (dac == 0 | aqmd == 0) & CalendarYear == 2014, lcolor(cranberry) lpattern(dot) 
		||  kdensity gpm if (dac == 0 | aqmd == 0) & CalendarYear == 2015, lcolor(cranberry) lpattern(shortdash)  
		||  kdensity gpm if (dac == 0 | aqmd == 0) & CalendarYear == 2016, lcolor(cranberry) lpattern(longdash)  
		||  kdensity gpm if (dac == 0 | aqmd == 0) & CalendarYear == 2017, lcolor(cranberry)   
		, graphregion(color(white)) 
		legend(label(1 "2014") label(2 "2015") label(3 "2016") label(4 "2017")) ytitle("Kernel Density - GPM") xtitle("Gallons Per Mile")
		yscale(range(0 200)) ylabel(0 (50) 200);
	graph export "${EFMPFleetRes}/kdensity_gpm_untreated_by_year.png", replace;
	#delimit cr


/*Online Appendix Figure A.1*/

import delimited "${MapFiles}/CA_Census_Tracts_to_ZIP_codes_2010.txt", clear
tostring v1, format(%12.0f) generate(tract)
tostring v2, generate(OwnerZipCode)
drop v1 v2
bys tract: egen N = nvals(OwnerZipCode)
bys tract: egen Area = total(v3)
bys tract: keep if _n == 1
drop v3
histogram N, graphregion(color(white)) discrete frac xtitle("Number of Zip Codes") ytitle("Fraction of Tracts")
graph export "${EFMPFleetRes}/Distribution_overlap_zips.png", replace


